package com.jxd.project.absence.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxd.project.absence.dao.IDeptDao;
import com.jxd.project.absence.dao.IEmpDao;
import com.jxd.project.absence.model.Dept;
import com.jxd.project.absence.model.Emp;
import com.jxd.project.absence.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DeptServiceImpl
 * @Description TODO
 * @Author 李杨
 * @Date 2025/10/10 17:30
 * @Version 1.0
 */
@Service
public class DeptServiceImpl extends ServiceImpl<IDeptDao, Dept> implements IDeptService {
    @Autowired
    private IDeptDao deptDao;
    @Autowired
    private IEmpDao empDao;

    @Override
    public boolean addDept(Dept dept) {
        return deptDao.insertDept(dept);
    }

    @Override
    public Map<String, Object> getDept(Map<String, String> queryMap) {
        Map<String, Object> result = new HashMap<>();

        String deptno = queryMap.get("deptno");
        String dname = queryMap.get("dname");
        String page = queryMap.get("page");
        String limit = queryMap.get("limit");

        if (dname == null) dname = "";

        if (page != null && limit != null) {
            IPage<Dept> deptPage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
            deptPage = deptDao.selectPage(deptPage, dname, deptno);
            result.put("data", deptPage.getRecords());
            result.put("count", deptPage.getTotal());
            result.put("code", 0);
            return result;
        } else {
            result.put("data", (Object)null);
            result.put("code", "500");
            result.put("msg", "参数不符");
            result.put("count", (Object)null);
            return result;
        }
    }

    @Override
    public boolean deltetDept(List<Integer> deptnos) {
        UpdateWrapper<Dept> uw = new UpdateWrapper<>();
        uw.in("deptno", deptnos)
                .set("isdelete", 1);
        UpdateWrapper<Emp> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("deptno", deptnos)
                .set("deptno", 0);
        empDao.update(null, updateWrapper);
        return deptDao.update(null, uw) > 0;
    }
}
